@layout("/common/_container.html"){
<link href="${ctxPath}/static/css/plugins/ionRangeSlider/ion.rangeSlider.css" rel="stylesheet">
<link href="${ctxPath}/static/css/plugins/ionRangeSlider/ion.rangeSlider.skinFlat.css" rel="stylesheet">

<script src="${ctxPath}/static/js/plugins/ionRangeSlider/ion.rangeSlider.min.js"></script>
<script src="${ctxPath}/static/js/common/range-object.js"></script>
<script src="${ctxPath}/static/js/common/Calc.js"></script>

<div class="row">
    <div class="col-sm-12">
        <div class="ibox float-e-margins">
            <div class="ibox-title">
                <h5>权重管理</h5>
            </div>
            <div class="ibox-content">
                <form class="form-horizontal">
                    <div class="form-group">
                        <label for="indication-slider-0" class="col-sm-2 control-label">权重和</label>
                        <div class="col-sm-10">
                            <input type="text" id="indication-slider-0" class="js-range-slider" value=""/>
                        </div>
                    </div>

                    @for(item in items){
                    <div class="form-group">
                        <label for="indication-slider-${item.id}" class="col-sm-2 control-label">${item.name}</label>
                        <div class="col-sm-10">
                            <input type="hidden" name="id" id="indication_${item.id}" value="${item.id}">
                            <input type="hidden" name="weight" id="indication_weight_${item.id}" value="${item.weight}">
                            <input type="text" id="indication-slider-${item.id}" class="js-range-slider" value=""/>
                        </div>
                    </div>
                    @}
                </form>
                <#button btnCss="info" name="权重和归1" id="ensure" icon="fa-check"
                clickFun="IndicationWeight.resetTotalWeight()"/>
                <#button btnCss="info" name="均分权重" id="ensure" icon="fa-check"
                clickFun="IndicationWeight.avgWeight()"/>
                <#button btnCss="info" name="提交" id="ensure" icon="fa-check"
                clickFun="IndicationWeight.addSubmit()"/>
                <#button btnCss="danger" name="取消" id="cancel" icon="fa-eraser"
                clickFun="IndicationWeight.close()"/>
            </div>
        </div>
    </div>
</div>


<script>

    var IndicationWeight={
        range_instance_0:null,
        range_instance_arr:[],
    };

    //提交
    IndicationWeight.addSubmit = function(){
        var ids = $("input[name='id']").map(function () {
           return $(this).val();
        })
        var weights = $("input[name='weight']").map(function () {
            return $(this).val() ? $(this).val() : 0;
        })

        var jsonStr = "[";
        for(var i =0;i< ids.length; i++){
            jsonStr+="{";
            jsonStr = jsonStr + "'id':"+ids[i] + "," + "'weight':"+weights[i];
            jsonStr +="}";
        }
        jsonStr+="]"

        $.ajax({
            url: Feng.ctxPath + "/indication/weight/update",
            type: "POST",
            contentType : 'application/json;charset=utf-8', //设置请求头信息
            dataType:"json",
            data: jsonStr,
            success: function(data){
                Feng.success("修改成功!");
                window.parent.Indication.table.refresh();
                IndicationWeight.close();
            },
            error: function(data){
                Feng.error("修改失败!" + data.responseJSON.message + "!");
            }
        });


    }

    IndicationWeight.close = function () {
        parent.layer.close(window.parent.Indication.layerIndex);
    }

    IndicationWeight.resetTotalWeight = function(){
        IndicationWeight.updateWeight(null,1, this.getTotalWeight());
    }

    IndicationWeight.getTotalWeight = function () {
        var total = 0;
        for(var i in IndicationWeight.range_instance_arr){
            total = Calc.Add(total, IndicationWeight.range_instance_arr[i].result.from);
        }
        return total;
    };

    IndicationWeight.avgWeight = function () {
        var size = IndicationWeight.range_instance_arr.length;
        var val = 1 / size;
        IndicationWeight.range_instance_arr.forEach(function (el) {
            el.update({
                from: val
            })
        })
    };

    IndicationWeight.updateTotal = function (totalWeight) {
        IndicationWeight.range_instance_0.update({
            from: totalWeight
        })
    }

    IndicationWeight.updateWeight = function(changedRangeInstance,before,cur) {
        var change = Calc.Sub(cur,before);
        var addV = cur > before ? -0.01 : 0.01 ;
        while (change != 0) {
            var mark = change;
            for(var i in IndicationWeight.range_instance_arr){
                if (change == 0) {
                    break;
                }
                if (IndicationWeight.range_instance_arr[i] != changedRangeInstance ) {
                    var from = IndicationWeight.range_instance_arr[i].result.from;
                    from = Calc.Add(from, addV);
                    if (from < 0 || from > 1) {
                        continue;
                    }
                    change = Calc.Add(change, addV);
                    IndicationWeight.range_instance_arr[i].update({
                        from:from
                    });
                }
            }
            if (mark === change) {
                break;
            }
        }
        IndicationWeight.updateTotal(IndicationWeight.getTotalWeight());
    }

    $(function () {

        IndicationWeight.range_instance_0 = new $Range({
            id:'indication-slider-0',
            v:1,
            max:100,
            disable:true,
        }).init();


        @for(item in items){
            var weight = 0;
            @if(isNotEmpty(item.weight)){
                    weight="${item.weight}"
            @}

            var range_instance_${item.id} = new $Range({
                id:"indication-slider-${item.id}",
                v:weight,
                onChange:function(data) {
                    var beforeValue = $("#indication_weight_${item.id}").val();
                    $("#indication_weight_${item.id}").val(data.from);
                    IndicationWeight.updateWeight(range_instance_${item.id},beforeValue,data.from);
                },
                onUpdate:function (data) {
                    $("#indication_weight_${item.id}").val(data.from);
                }
            }).init();

            IndicationWeight.range_instance_arr.push(range_instance_${item.id});
        @}

        var totalWeight = IndicationWeight.getTotalWeight();
        IndicationWeight.updateTotal(totalWeight);
}())

</script>
@}
